package com.wei.duowan.bbs.ui.activity;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.WebViewBean;
import com.wei.duowan.bbs.presenter.BasePresenter;

import butterknife.Bind;

/**
 * 作者：赵若位
 * 时间：2019/8/2 14:47
 * 联系方式：1070138445
 * 功能：
 */
public class WebViewActivity extends BaseActivity
{
    @Bind(R.id.progressBar)
    ProgressBar mProgressBar;
    @Bind(R.id.webView)
    WebView mWebView;

//    private WebViewBean mData;

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.activity_web;
    }

    @Override
    protected void initView()
    {
        mProgressBar.setMax(100);
        mProgressBar.setProgress(20);
        initWebSetting();
//        mData = getIntent().getParcelableExtra(TAG);
        mWebView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String request)
            {
                view.loadUrl(request);
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
//        mData.setUrl("http://dastat.duowan.com/bbs/185-1740-2.do?url=http%3A%2F%2Fsy.duowan.com%2Fgame%2F73115.html&os=android");
//        mData.setUrl("http://bbs.duowan.com/forum-1181-1.html");
        mWebView.setDownloadListener((s, s1, s2, s3, l) ->
        {
            Log.e(TAG, "onDownloadStart: " + s);
            Log.e(TAG, "onDownloadStart: " + s1);
            Log.e(TAG, "onDownloadStart: " + s2);
            Log.e(TAG, "onDownloadStart: " + s3);
            Log.e(TAG, "onDownloadStart: " + l);
        });
//        mWebView.loadUrl("http://bbs.duowan.com/forum-1181-1.html");
        mWebView.loadUrl("http://134.175.255.85:81/?grant_type=client_credential&apiUId=0391e39b-7681-405b-915f-cf98ada1065b&apiSecret=SbHZGW0cznTksGhOrPELl0X7mAszw8SD&projectId=2#/index/home");
//        mWebView.loadUrl("http://dastat.duowan.com/bbs/185-1740-2.do?url=http%3A%2F%2Fsy.duowan.com%2Fgame%2F73115.html&os=android");
    }

    /*初始化WebSetting相关操作*/
    private void initWebSetting()
    {
        WebSettings settings = mWebView.getSettings();
        settings.setUserAgentString("DuowanBbs_Android");
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        //支持JS脚本
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setTextZoom(100);
        settings.setBuiltInZoomControls(false);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        //启用数据库
        settings.setDatabaseEnabled(true);
        String dir = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        //设置定位的数据库路径
        settings.setGeolocationDatabasePath(dir);

        int minimumFontSize = 8;
        int minimumLogicalFontSize = 8;
        int defaultFontSize = 16;
        int defaultFixedFontSize = 13;

        settings.setMinimumFontSize(minimumFontSize);
        settings.setMinimumLogicalFontSize(minimumLogicalFontSize);
        settings.setDefaultFontSize(defaultFontSize);
        settings.setAllowFileAccess(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDefaultFixedFontSize(defaultFixedFontSize);
        //开启DomStorage缓存
        settings.setDomStorageEnabled(true);
        //启用地理定位
        settings.setGeolocationEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
        {
            //自动播放，不需要用户确认
            settings.setMediaPlaybackRequiresUserGesture(false);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

}
